package com.yixiu.study.juc.phaser;

import java.util.Random;
import java.util.concurrent.Phaser;
import java.util.concurrent.TimeUnit;

/**
 * @CalssName T01_Phaser
 * @Description 分阶段栅栏
 * @Author yixiu
 * @Date 2021/6/25 9:39
 * @Verison num1.0
 **/
public class T01_Phaser {

    static Random r = new Random();
    static MarriagePhaser phaser = new MarriagePhaser();


    static void millisSleep(int milli){
        try {
            TimeUnit.MILLISECONDS.sleep(milli);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        phaser.bulkRegister(7);

        for(int i=0; i<5; i++){
            new Thread(new Person("p" + i)).start();
        }

        new Thread(new Person("新郎")).start();
        new Thread(new Person("新娘")).start();

    }


    static class MarriagePhaser extends Phaser {
        @Override
        protected boolean onAdvance(int phase, int registeredParties){
            switch (phase) {
                case 0:
                    System.out.println("所有人都到齐了！" + registeredParties);
                    System.out.println();
                    return false;
                case 1:
                    System.out.println("所有人都吃完了！" + registeredParties);
                    System.out.println();
                    return false;
                case 2:
                    System.out.println("所有人都离开了！" + registeredParties);
                    System.out.println();
                    return false;
                case 3:
                    System.out.println("婚礼结束！新郎新娘洞房！" + registeredParties);
                    System.out.println();
                    return false;
                default:
                    return false;
            }
        }
    }

    static class Person implements Runnable{
        String name;

        public Person(String name){
            this.name = name;
        }

        public void arrive(){
            millisSleep(r.nextInt(1000));
            System.out.printf("%s 到达现场！\n", name);
            phaser.arriveAndAwaitAdvance();
        }

        public void eat(){
            millisSleep(r.nextInt(1000));
            System.out.printf("%s 吃完！\n", name);
            phaser.arriveAndAwaitAdvance();
        }

        public void leaf(){
            millisSleep(r.nextInt(1000));
            System.out.printf("%s 离开！\n", name);
            phaser.arriveAndAwaitAdvance();
        }

        private void hug(){
            if(name.equals("新郎") || name.equals("新娘")){
                millisSleep(r.nextInt(1000));
                System.out.printf("%s 洞房！\n", name);
                phaser.arriveAndAwaitAdvance();
            } else {
                phaser.arriveAndDeregister();
//                phaser.register();
            }
        }

        @Override
        public void run() {
            //这边的编码顺序需要严格保证，才会对应上方的 0 num1 num2 3
            arrive();
            eat();
            leaf();
            hug();
        }
    }

}
